// pages/order/order.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isactive:1,
    pageNo:1,
    pageSize:10,
    imgurl:app.globalData.Imageurl,
    showtype:1,
    isempty:false,
    payStats:'',
    orderStats:'',
    wholelist:'',
    dropdown:true,
    isfeli:false,
    isloding:false,
    papenaxtto:0,
    pagecount:0,
    isloadcomplete:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sjs=this.randomWord()
    var datatime = Date.parse(new Date());
    var tokens=''+sjs+''+datatime+''+app.globalData.userId
    this.setData({
      token:tokens
    })
    // debugger
    console.log(options)
    if(options.showtype==2){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      this.setData({
        showtype:options.showtype,
        isactive:2,
        payStats:0,
        orderStats:'',
        wholelist:'',
        })

        this.getGoodsOrderPage(this.data.orderStats,this.data.payStats)
    }else if(options.showtype==3){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      this.setData({
        showtype:options.showtype,
        isactive:3,
        payStats:"",
        orderStats:1,
        wholelist:'',
        })
        this.getGoodsOrderPage(1,'')
    }else if(options.showtype==4){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      this.setData({
        showtype:options.showtype,
        isactive:4,
        payStats:"",
        orderStats:2,
        wholelist:'',
        })
        this.getGoodsOrderPage(2,'')
    }else if(options.showtype==5){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      this.setData({
        showtype:options.showtype,
        isactive:5,
        payStats:"",
        orderStats:3,
        wholelist:'',
        })
        this.getGoodsOrderPage(3,'')
    }else if(options.showtype==1){
      // wx.showLoading({
      //   title: '加载中',
      // })
      this.setData({
        payStats:"",
        orderStats:'',
        wholelist:'',
        })

      this.getGoodsOrderPage(this.data.orderStats,this.data.payStats)
    }
   

  },
  gaib(){
        this.setData({
          isactive:5
        })
},
//订单去详情
goorderxq(e){
  console.log(e)
  switch (e.currentTarget.dataset.goodtype) {
    case '0':
      console.log('待付款')
      if (e.currentTarget.dataset.paystats==0) {
        wx.navigateTo({
          url: '/pages/unpaid/unpaid?orderId='+e.currentTarget.dataset.id,
        })
      }
      
      
      break;
      case '1':
        console.log('待发货')
        wx.navigateTo({
          url: '/pages/orderdelivery/orderdelivery?orderId='+e.currentTarget.dataset.id,
        })
      
      break;
      case '-1':
        console.log('已取消')
        wx.navigateTo({
          url: '/pages/completepaid/completepaid?orderId='+e.currentTarget.dataset.id+'&type=1',
        })
      
      break;
      case '2':
        console.log('已发货')
        wx.navigateTo({
          url: '/pages/orderreceipt/orderreceipt?orderId='+e.currentTarget.dataset.id,
        })
      break;
      case '3':
        console.log('待评价')
        wx.navigateTo({
          url: '/pages/paid/paid?orderId='+e.currentTarget.dataset.id,
        })
      break;
      case "4":
        wx.navigateTo({
          url: '/pages/completepaid/completepaid?orderId='+e.currentTarget.dataset.id+'&type=2',
        })
      break;
  
    default:
      break;
  }
},


//去详情按钮
gopayment(e){
  console.log(e.currentTarget.dataset.id) 
  //goodtype类型
  //订单状态 0:待付款 1:待发货 2:已发货 3:已完成  4:已评价
  switch (e.currentTarget.dataset.goodtype) {
    case '0':
      console.log('待付款')
      wx.navigateTo({
        url: '/pages/unpaid/unpaid?orderId='+e.currentTarget.dataset.id,
      })
      
      break;
      case '1':
        console.log('待发货')
        
        wx.navigateTo({
          url: '/pages/orderdelivery/orderdelivery?orderId='+e.currentTarget.dataset.id,
        })
      
      break;
      case '2':
        console.log('已发货')
        wx.navigateTo({
          url: '/pages/orderreceipt/orderreceipt?orderId='+e.currentTarget.dataset.id,
        })
      break;
      case '3':
        console.log('已完成 ')
        wx.navigateTo({
          url: '/pages/paid/paid?orderId='+e.currentTarget.dataset.id,
        })
      break;
      case "4":
        console.log('已评价 ')
        this.godeleorder(e.currentTarget.dataset.id)

      break;
  
    default:
      break;
  }

  // if (e.currentTarget.dataset.goodtype) {
    
  // }
  
},
orderstatus(e){
  var t=this
  console.log(e.currentTarget.dataset.actid)
  switch (e.currentTarget.dataset.actid) {
      case '1':
        if (t.data.isactive!=1) {
          wx.showLoading({
            title: '加载中',
            mask:true
          })
          t.setData({
            isactive:1,
            showtype:1,
            pageNo:1,
            payStats:'',
            orderStats:'',
            wholelist:'',
            pagecount:0,
           })
           t.getGoodsOrderPage(t.data.orderStats,t.data.payStats)
        }
      break;
      case '2':
        if (t.data.isactive!=2) {
          wx.showLoading({
            title: '加载中',
            mask:true
          })
          t.setData({
            isactive:2,
            showtype:2,
            pageNo:1,
            payStats:0,
            orderStats:'',
            wholelist:'',
            pagecount:0
           })
           t.getGoodsOrderPage(t.data.orderStats,t.data.payStats)
        }
      
      break;
      case '3':
        if (t.data.isactive!=3) {
          wx.showLoading({
            title: '加载中',
            mask:true
          })
          t.setData({
            isactive:3,
            showtype:3,
            pageNo:1,
            payStats:'',
            orderStats:1,
            wholelist:'',
            pagecount:0
           })
           t.getGoodsOrderPage(t.data.orderStats,t.data.payStats)
        }
      
      break;
      case '4':
        if (t.data.isactive!=4) {
          wx.showLoading({
            title: '加载中',
            mask:true
          })
          t.setData({
            isactive:4,
            showtype:4,
            pageNo:1,
            payStats:'',
            orderStats:2,
            wholelist:'',
            pagecount:0
           })
           t.getGoodsOrderPage(t.data.orderStats,t.data.payStats)
        }
      
      break;
      case '5':
        if (t.data.isactive!=5) {
          wx.showLoading({
            title: '加载中',
            mask:true
          })
          console.log('1123')
          t.setData({
            isactive:5,
            showtype:5,
            pageNo:1,
            payStats:'',
            orderStats:3,
            wholelist:'',
            pagecount:0
           })
           t.getGoodsOrderPage(t.data.orderStats,t.data.payStats)
        }
      break;
  
    default:
      break;
  }
},
//退款未发货
gorefund(e){
    var t=this
    // console.log(e)
    wx.showModal({
      title: '申请售后',
      content: '是否申请该订单退款',
      success (res) {
        if (res.confirm) {

          wx.request({
            url: app.globalData.Interfaceurl + 'applyGoodsOrderRefund',
            method: "post",
            header: {
                "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
            },
            data: {
                userId: app.globalData.userId,
                orderId:e.currentTarget.dataset.id,
                token:t.data.token

            },
            success: res => {
                console.log(res)
                t.setData({
                  wholelist:'',
                  isactive:1,
                  pageNo:1,
                  orderStats:'',
                  payStats:''
                })
                t.getGoodsOrderPage('','')

            }
        })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  console.log(e)
  

},


//退款已收货
gorefundxq(e){
  wx.navigateTo({
    url: '/pages/paid/paid?orderId='+e.currentTarget.dataset.id,
  })
},

//取消订单
gocancelorder(e){
  var t=this
  wx.showModal({
    title:'提示',
    content:'您将取消订单',
    success (res) {
      if (res.confirm) {
        wx.request({
          url: app.globalData.Interfaceurl + 'cancelGoodsOrder',
          method: "post",
          header: {
              "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
          },
          data: {
              userId: app.globalData.userId,
              orderId:e.currentTarget.dataset.id,             
          },
          success: res => {
              console.log(res)
              wx.showToast({
                title: '取消成功',
              })
              t.setData({
                wholelist:'',
                isactive:1,
                pageNo:1,
                orderStats:'',
                payStats:'',
              })
              t.getGoodsOrderPage('','')
          }
      })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
  
},
//确认收货
confirmsh(e){
  var t=this
  wx.showModal({
    title:'提示',
    content:'您将确认收货',
    success (res) {
      if (res.confirm) {
        wx.request({
          url: app.globalData.Interfaceurl + 'confirmGoodsOrderArrival',
          method: "post",
          header: {
              "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
          },
          data: {
              userId: app.globalData.userId,
              orderId:e.currentTarget.dataset.id,             
          },
          success: res => {
              console.log(res)
              wx.showToast({
                title: '确认成功',
              })
              t.setData({
                wholelist:'',
                isactive:5,
                pageNo:1,
                showtype:5,
              })
              t.getGoodsOrderPage(3,'')
              
          }
      })


      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
//删除订单
godeleorder(e){
  var t=this
  wx.showModal({
    title:'提示',
    content:'您将删除该订单',
    success (res) {
      if (res.confirm) {
        wx.request({
          url: app.globalData.Interfaceurl + 'delGoodsOrder',
          method: "post",
          header: {
              "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
          },
          data: {
              userId: app.globalData.userId,
              orderId:e.currentTarget.dataset.id,
             
          },
          success: res => {
              console.log(res)
              wx.showToast({
                title: '删除成功',
              })
              t.setData({
                wholelist:'',
                isactive:1,
                pageNo:1,
              })
              t.getGoodsOrderPage()
              
          }
      })


      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
  
},


/**
   * userId 用户id
   * payStats 0:待付款
   * payStats 1:待发货 2:待收货 3:待评价
   * 
   */
getGoodsOrderPage(myorderStats='',mypayStats=''){
  var t=this
  wx.request({
    url: app.globalData.Interfaceurl + 'getGoodsOrderPage',
    method: "post",
    header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    },
    data: {
        userId: app.globalData.userId,
        orderStats:myorderStats,
        payStats:mypayStats,
        pageNo:t.data.pageNo,
        pageSize:t.data.pageSize,
    },
    success: res => {
        console.log(t.data.wholelist)
        console.log(res)
        if(res.data.result=='0'){
          wx.hideLoading()
          t.setData({
            isloadcomplete:false,
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }

        // if (myorderStats==''&&mypayStats=='') {
          // console.log("111")
          if (t.data.wholelist!='') {

            t.setData({
              wholelist:t.data.wholelist.concat(res.data.page.list),
            })
            
          }else{
            t.setData({
              wholelist:res.data.page.list,
            })
          }
          //无数据
        
          //console.log('pagecount',t.data.pagecount)

         

          if (res.data.page.list.length>0) {

            t.setData({
              pagecount:Math.ceil(parseInt(res.data.page.count)/parseInt(t.data.pageSize)),
              isempty:false,
              

            })//
            if (t.data.pageNo<t.data.pagecount) {
              if(t.data.isactive==1){
              t.setData({
                isfeli:false,
                isloding:true
              })
            }else{
              t.setData({
                isfeli:false,
                isloding:false
              })
            }
            }else{
              if(t.data.isactive==1){
              t.setData({
                isfeli:true,
                isloding:false
              })
              }else{
                t.setData({
                  isfeli:false,
                  isloding:false
                })
              }
            }
            
            
          }else{
            if(t.data.isactive==1){
              t.setData({
                isloding:false,
                isfeli:false
              })
            }else{
              t.setData({
                isfeli:false,
                isloding:false
              })
            }

            t.setData({
              
              isempty:true,
            })
          }
        // }
        
        
        
    }
})
},
//生成随机数
randomWord() {
    var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
     var nums = "";
      for (var i = 0; i < 20; i++) {
      var id = parseInt(Math.random() * 61);
      nums += chars[id]
      }
      return nums; 
      },
//去商品列表
goclass(){
  wx.switchTab({
    url: '/pages/index/index',
  })

},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.papenaxtto==1){
      console.log('111')
      this.setData({
        showtype:5,
        isactive:5,
        payStats:"",
        orderStats:3,
        wholelist:'',
        pagecount:0
        })
        this.getGoodsOrderPage(3,'')
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  console.log(this.data.pageNo)
  console.log(this.data.pagecount)
    if (this.data.pageNo <this.data.pagecount) {
      console.log('2221')
      this.setData({
        pageNo:this.data.pageNo+1
      })
    //   payStats:'',
    // orderStats:'',
    
      this.getGoodsOrderPage(this.data.orderStats,this.data.payStats)
      
    }else{
      // this.setData({
      //   isfeli:true,
      //   isloding:false
      // })

    }
    

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})